package com.txzt.gaea.business.project.service.impl;

import com.txzt.gaea.business.project.domain.model.ProjectInfo;
import com.txzt.gaea.business.project.domain.model.ProjectNode;
import com.txzt.gaea.business.project.service.IProjectInfoService;
import com.txzt.gaea.business.project.service.IProjectNodeService;
import com.txzt.gaea.business.util.TranslateUtil;
import com.txzt.gaea.common.base.service.impl.BaseServiceImpl;
import com.txzt.gaea.common.enums.FlowFlagEnum;
import com.txzt.gaea.common.enums.IsDeleteEnum;
import com.txzt.gaea.common.enums.NodeStatus;
import com.txzt.gaea.common.enums.ProjectStatus;
import com.txzt.gaea.flow.service.FlowService;
import com.txzt.gaea.flow.vo.FlowBizStepVo;
import com.txzt.gaea.provide.service.FileInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ProjectNodeServiceImpl extends BaseServiceImpl<ProjectNode> implements IProjectNodeService {

    @Autowired
    private FlowService flowService;
    @Autowired
    private FileInfoService fileInfoService;
    @Autowired
    private IProjectInfoService projectInfoService;


    @Override
    public ProjectNode selectProjectNodeById(Long id) {
        ProjectNode projectNode = sqlManager.single(ProjectNode.class,id);
        //查询项目节点当前审核流程步骤
        List<FlowBizStepVo> flowStep = flowService.getFlowBizStep(projectNode.getFlowId());
        //判断当前操作人是否可审核流程
        projectNode.setCheck(TranslateUtil.getCheck(flowStep));
        projectNode.setFlowStep(flowStep);
        return projectNode;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int saveProjectNode(ProjectNode projectNode) {
        projectNode.setNodeStatus(NodeStatus.CHECK.getCode());
        projectNode.setIsDelete(IsDeleteEnum.NOTDELETE.getCode());
        int count = sqlManager.insertTemplate(projectNode,true);
        //开启流程
        Long flowBizId = flowService.startFlow(projectNode.getId(), "tb_project_node","node_status",
                NodeStatus.BACK.getCode(), NodeStatus.FINISH.getCode(), FlowFlagEnum.MAJOR_NODE.getCode());
        ProjectNode target = new ProjectNode();
        target.setId(projectNode.getId());
        target.setFlowId(flowBizId);
        sqlManager.updateTemplateById(target);
        //处理文件
        ProjectInfo projectInfo = projectInfoService.selectById(projectNode.getProjectId());
        fileInfoService.fileChange(null,projectNode.getNodeFile(),projectInfo.getProjectNo(),2);
        return count;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int editProjectNode(ProjectNode projectNode) {
        ProjectNode ori = sqlManager.single(ProjectNode.class,projectNode.getId());
        //处理文件
        ProjectInfo projectInfo = projectInfoService.selectById(projectNode.getProjectId());
        fileInfoService.fileChange(ori.getNodeFile(),projectNode.getNodeFile(),projectInfo.getProjectNo(),2);
        return sqlManager.updateTemplateById(projectNode);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public int resubmitProjectNode(ProjectNode projectNode) {
        //开启流程
        Long flowBizId = flowService.startFlow(projectNode.getId(), "tb_project_node","node_status",
                NodeStatus.BACK.getCode(), NodeStatus.FINISH.getCode(), FlowFlagEnum.MAJOR_NODE.getCode());
        ProjectNode ori = sqlManager.single(ProjectNode.class,projectNode.getId());
        projectNode.setNodeStatus(NodeStatus.CHECK.getCode());
        projectNode.setFlowId(flowBizId);
        int count = sqlManager.updateTemplateById(projectNode);
        //处理文件
        ProjectInfo projectInfo = projectInfoService.selectById(projectNode.getProjectId());
        fileInfoService.fileChange(ori.getNodeFile(),projectNode.getNodeFile(),projectInfo.getProjectNo(),2);
        return count;
    }
}
